###Install packages *** ###Load libraries
library(raster)
Loading required package: sp
library(tidyverse)
[30m── [1mAttaching packages[22m ───────────────────────────────────────────────────────────── tidyverse 1.2.1 ──[39m
[30m[32m✔[30m [34mggplot2[30m 3.1.0 [32m✔[30m [34mpurrr [30m 0.2.5
[32m✔[30m [34mtibble [30m 2.0.0 [32m✔[30m [34mdplyr [30m 0.7.8
[32m✔[30m [34mtidyr [30m 0.8.2 [32m✔[30m [34mstringr[30m 1.3.1
[32m✔[30m [34mreadr [30m 1.3.1 [32m✔[30m [34mforcats[30m 0.3.0[39m
package ‘tibble’ was built under R version 3.5.2[30m── [1mConflicts[22m ──────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
[31m✖[30m [34mtidyr[30m::[32mextract()[30m masks [34mraster[30m::extract()
[31m✖[30m [34mdplyr[30m::[32mfilter()[30m masks [34mstats[30m::filter()
[31m✖[30m [34mdplyr[30m::[32mlag()[30m masks [34mstats[30m::lag()
[31m✖[30m [34mdplyr[30m::[32mselect()[30m masks [34mraster[30m::select()[39m
library(ggthemes)
library(patchwork)
library(maptools)
Checking rgeos availability: TRUE
library(ggmap)
Google Maps API Terms of Service: http://developers.google.com/maps/terms.
Please cite ggmap if you use it: see citation("ggmap") for details.
library(marmap)
Attaching package: ‘marmap’
The following object is masked from ‘package:raster’:
as.raster
The following object is masked from ‘package:grDevices’:
as.raster
###Part 1: Outline maps
usa <- map_data(map="state")
Attaching package: ‘maps’
The following object is masked from ‘package:purrr’:
map
us_missouri <- map_data(map="state",
region="missouri")
us_other <- subset(x=usa,
subset=region %in% c("alabama",
"florida",
"mississippi",
"north carolina"))
str(usa)
'data.frame': 15537 obs. of 6 variables:
$ long : num -87.5 -87.5 -87.5 -87.5 -87.6 ...
$ lat : num 30.4 30.4 30.4 30.3 30.3 ...
$ group : num 1 1 1 1 1 1 1 1 1 1 ...
$ order : int 1 2 3 4 5 6 7 8 9 10 ...
$ region : chr "alabama" "alabama" "alabama" "alabama" ...
$ subregion: chr NA NA NA NA ...
ggplot()+
geom_polygon(data=us_other,
aes(x=long,
y=lat,
group=group),
fill = "gray")
ggplot()+
geom_polygon(data = us_other,
aes(x=long,
y=lat,
group=group),
fill="gray")+
geom_polygon(data=us_missouri,
aes(x=long,
y=lat,
group=group),
fill="black")
ggplot()+
geom_polygon(data = us_other,
aes(x=long,
y=lat,
group=group),
fill="gray")+
geom_polygon(data=us_missouri,
aes(x=long,
y=lat,
group=group),
fill="black")+
geom_polygon(data = usa,
aes(x=long,
y=lat,
group=group),
fill=NA,
color="black")
us_map <- ggplot()+
geom_polygon(data = us_other,
aes(x=long,
y=lat,
group=group),
fill="gray")+
geom_polygon(data=us_missouri,
aes(x=long,
y=lat,
group=group),
fill="black")+
geom_polygon(data=usa,
aes(x=long,
y=lat,
group=group),
fill=NA,
color="black")+
theme_map()+
coord_fixed(1.3)
us_map
#Missouri map
missouri <- map_data("county", "missouri")
mo_scott <- subset(missouri, subregion %in% c("scott"))
mo_map <- ggplot()+
geom_polygon(data=missouri, aes(x=long, y=lat, group=group), fill=NA, color="black")+
geom_polygon(data=mo_scott, aes(x=long, y=lat, group=group), fill="black")+
theme_map()+
coord_fixed(1.3)
mo_map
us_map + mo_map + plot_layout(ncol = 2, widths = c(1.5, 1))
#Outline Maps #This should be interesting… I was born in Hawaii. It isn’t part of the map_data, so I’ll choose Utah, where I lived for two years and learned to ski. I lived in Ogden in Weber county. My initials are AKC. The counties I chose to put in gray are Kane and Carbon.
us_utah <- map_data(map="state",
region = "utah")
utah <- map_data("county", "utah")
ut_birth <- subset(utah, subregion %in% c("weber"))
ut_names <- subset(utah, subregion %in% c("carbon", "kane"))
akc_usa <- ggplot()+
geom_polygon(data = us_utah,
aes(x = long,
y = lat,
group = group),
fill = "black")+
geom_polygon(data = usa,
aes(x = long,
y = lat,
group = group),
fill = NA,
color = "black")+
theme_map()+
coord_fixed(1.3)
akc_counties <- ggplot()+
geom_polygon(data = ut_birth,
aes(x = long,
y = lat,
group = group),
fill = "black")+
geom_polygon(data = ut_names,
aes(x = long,
y = lat,
group = group),
fill = "gray")+
geom_polygon(data = utah,
aes(x = long,
y = lat,
group = group),
fill = NA,
color = "black")+
theme_map()+
coord_fixed(1.3)
akc_usa + akc_counties + plot_layout(ncol = 2, widths = c(1.5,1))
#The Hawaii Gambit… Born on Oahu, and have visited Hawaii (the big island) and Maui. I’ll see if I can get that to work. I love working with maps.
hawaii <- map_data("world", "USA:Hawaii")
hawaii_oahu <- map_data("world", "USA:Hawaii:Oahu")
other_islands <- map_data("world", c("USA:Hawaii:Hawaii", "USA:Hawaii:Maui"))
akc_usa <- ggplot()+
geom_polygon(data = us_utah,
aes(x = long,
y = lat,
group = group),
fill = "black")+
geom_polygon(data = usa,
aes(x = long,
y = lat,
group = group),
fill = NA,
color = "black")+
theme_map()+
coord_fixed(1.3)
akc_hawaii <- ggplot()+
geom_polygon(data = hawaii,
aes(x = long,
y = lat,
group = group),
fill = NA,
color = "black")+
geom_polygon(data = hawaii_oahu,
aes(x = long,
y = lat,
group = group),
fill = "black")+
geom_polygon(data = other_islands,
aes(x = long,
y = lat,
group = group),
fill = "gray")+
theme_map()+
coord_fixed(1.3)
akc_usa + akc_hawaii + plot_layout(ncol = 2, widths = c(1.5, 1))
#Hmm. The big island of Hawaii didn’t show up as gray. I guess the name in map_data is different. The code is there that would select it. On to Part 2!
###Part 2: Dot distribution maps ###Load the libraries #already loaded ###Define some variables. set.seed value as in assignment so I can see if I’m doing it right!
min_long <- -126
max_long <- -104
min_lat <- 31
max_lat <- 50
set.seed(9618973)
file_path <-"~/Documents/BI685Files/GitHubDocs/chronister_andrew/data"
###Read in the “Gigantopithecus” data
bigfoot <- read.csv(file.path(file_path, "bigfoot.csv"))
bigfoot <- filter(bigfoot,
long >= min_long & long <= max_long,
lat >= min_lat & lat <= max_lat) %>%
sample_n(300) %>%
mutate(name = "Gigantiopithecus") %>%
select(name, long, lat)
#Read in the bears data
bears <- read_csv(file.path(file_path, "bears.csv"))
Parsed with column specification:
cols(
name = [31mcol_character()[39m,
long = [32mcol_double()[39m,
lat = [32mcol_double()[39m
)
both_species <- bind_rows(bigfoot, bears)
head(both_species)
tail(both_species)
#Get terrain map
base = get_stamenmap(bbox = c(min_long,
min_lat,
max_long,
max_lat),
zoom = 4,
maptype = "terrain-background")
ggmap(base)
base <- ggmap(base)
base + geom_point(data = bigfoot,
aes(x = long,
y = lat))
base + geom_point(data = bears,
aes(x = long,
y = lat))
#Both species
base + geom_point(data = both_species,
aes(x = long,
y = lat,
shape = name,
fill = name),
size = 2)+
scale_shape_manual(values = c(22:23))+
labs(x = "Longitude",
y = "Latitude",
fill = "Species",
shape = "Species")
###Recreate with SO number as seed.
set.seed(1065450)
#I could just redo everything from here with the existing code and should get the new results.
bigfoot <- read_csv(file.path(file_path, "bigfoot.csv"))
Parsed with column specification:
cols(
long = [32mcol_double()[39m,
lat = [32mcol_double()[39m
)
bigfoot <- filter(bigfoot,
long >= min_long & long <= max_long,
lat >= min_lat & lat <= max_lat) %>%
sample_n(300) %>%
mutate(name = "Gigantopithecus") %>%
select(name, long, lat)
#Bears data already read in.
both_species <- bind_rows(bigfoot, bears)
head(both_species)
tail(both_species)
base = get_stamenmap(bbox = c(min_long,
min_lat,
max_long,
max_lat),
zoom = 4,
maptype = "terrain-background")
base <- ggmap(base)
base + geom_point(data = both_species,
aes(x = long,
y = lat,
shape = name,
fill = name),
size = 2)+
scale_shape_manual(values = c(22:23))+
labs(x = "Longitude",
y = "Latitude",
fill = "Species",
shape = "Species")
###Part3: Bathymetry ###Load the libraries #Already loaded ###Define the variables
min_long <- -170
max_long <- 164
min_lat <- -42
max_lat <- -8
earthquakes <- quakes %>%
mutate(depth = depth * 3280.84)
#load NOAA data
fiji <- getNOAA.bathy(lon1 = min_long,
lon2 = max_long,
lat1 = min_lat,
lat2 = max_lat,
antimeridian = TRUE,
keep = TRUE)
Querying NOAA database ...
This may take seconds to minutes, depending on grid size
Building bathy matrix ...
autoplot.bathy(fiji,
geom = c("raster", "contour"),
size = 0.3,
na.rm = TRUE)
Ignoring unknown parameters: size
autoplot(fiji,
geom = c("raster", "contour"),
size = 0.1,
na.rm = TRUE)+
scale_fill_gradient2(low = "dodgerblue4",
mid = "gainsboro",
high = "darkgreen",
name = "Depth (ft)")+
labs(x = "Longitude",
y = "Latitude")+
theme(axis.title.y = element_text(angle = 0,
vjust = 0.5))
Ignoring unknown parameters: size
base_map <- autoplot(fiji,
geom = c("raster", "contour"),
size = 0.1,
na.rm = TRUE)+
scale_fill_gradient2(low = "dodgerblue4",
mid = "gainsboro",
high = "darkgreen",
name = "Depth (ft)")+
labs(x = "Longitude",
y = "Latitude")+
theme(axis.title.y = element_text(angle = 0,
vjust = 0.5))
Ignoring unknown parameters: size
base_map + geom_point(data = earthquakes,
aes(x = long,
y = lat))
#Use alpha arguement
base_map + geom_point(data = earthquakes,
aes(x = long,
y = lat,
size = mag),
alpha = 0.4)
#scale size a little smaller
base_map + geom_point(data = earthquakes,
aes(x = long,
y = lat,
size = mag),
alpha = 0.4) +
scale_size_continuous(range = c(0.5, 3), name = "Magnitude")
###Now you try it. *** ###Set new variables
min_long <- -90
max_long <- -58
min_lat <- 8
max_lat <- 28
#Read in the data
blennies <- read_csv(file.path(file_path, "blennies.csv"))
Parsed with column specification:
cols(
name = [31mcol_character()[39m,
long = [32mcol_double()[39m,
lat = [32mcol_double()[39m
)
blennies
#Get the caribbean data
carib_sea <- getNOAA.bathy(lon1 = min_long,
lon2 = max_long,
lat1 = min_lat,
lat2 = max_lat,
antimeridian = FALSE,
keep = TRUE)
Querying NOAA database ...
This may take seconds to minutes, depending on grid size
Building bathy matrix ...
#Use autoplot for the base map
base_map <- autoplot(carib_sea,
geom = c("raster", "contour"),
size = 0.1,
na.rm = TRUE) +
scale_fill_etopo(guide = FALSE)+
labs(x = "Longitude",
y = "Latitude")+
theme(axis.title.y = element_text(angle = 0,
vjust = 0.5))
Ignoring unknown parameters: size
base_map
#Add the blennies
base_map + geom_point(data = blennies,
aes(x = long,
y = lat,
shape = name),
fill = "white",
size = 3.2)+
scale_shape_manual(values = c(21:23))+
labs(shape = "Species")+
theme(legend.position = "bottom")+
theme(legend.text = element_text(face = "italic"))